<template>
<view class="container">
  <view class="chongzhijine">
    <text>充值金额</text>
    <input :value="money" @input="getBuyerScript" placeholder></input>
  </view>
  <view class="chongzhiList">
    <view class="chongzhiItem" @click.stop="selectItemFun" :data-info="item" v-for="(item, index) in setting.platformSetting.rechargeList" :key="index" :style="'border:1rpx solid ' + setting.platformSetting.defaultColor + ';color:' + setting.platformSetting.defaultColor">
      <text class="price">￥{{item.value}}</text>
      <!-- <text class="rewardPrice" v-if="item.reward>0">赠送￥{{item.reward}}</text> -->
      <text class="rewardName">{{item.title}}</text>
      <text class="rewardPrice" :style="'background:'+setting.platformSetting.defaultColor" v-if="item.reward>0">赠￥{{item.reward}}</text>
    </view>
  </view>
  <view class="way">
   <view class="payName">支付方式</view>
  <radio-group @change="getPayWay">
   

    <label class="payWay">
      <radio :color="setting.platformSetting.defaultColor" checked="true" value="3"></radio>
      <text>微信支付</text>
    </label>
 <!--   <label class='payWay'>
      <radio color="{{setting.platformSetting.defaultColor}}" checked='false' value='4'></radio>
      <text>支付宝</text>
    </label> -->
  </radio-group>
  </view>

  <view></view>
  <button :loading="butn_show_loading" @tap="subMitButn" class="subMitButn" :style="'background:' + setting.platformSetting.defaultColor">立即充值</button>
</view>
</template>

<script>

export default {
  data() {
    return {
      money: 100,
      payway: 3,
      butn_show_loading: false,
      setting: "",
      loginUser: "",
      properties: ""
    };
  },

  components: {},
  props: {},

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    console.log("options", options);

    if (options.money) {
      that.setData({
        money: options.money
      });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setting = getApp().globalData.setting;

    if (this.setting.platformSetting.rechargeList && typeof this.setting.platformSetting.rechargeList == 'string') {
      this.setting.platformSetting.rechargeList = JSON.parse(this.setting.platformSetting.rechargeList);
    }

    this.setData({
      setting: this.setting,
      loginUser: getApp().globalData.loginUser,
      properties: getApp().globalData.properties
    });
    console.log("setting", getApp().globalData.setting);

    if (getApp().globalData.properties.alias_yue) {
      wx.setNavigationBarTitle({
        title: getApp().globalData.properties.alias_yue + '充值'
      });
    }
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},
  methods: {
    selectItemFun: function (e) {
      console.log("========selectItemFun========", e);
      let that = this;
      let info = e.currentTarget.dataset.info;
      that.money = info.value;
      that.setData({
        money: info.value
      });
      that.subMitButn();
    },
    getBuyerScript: function (e) {
      this.setData({
        money: e.detail.value
      });
    },
    getPayWay: function (e) {
      this.setData({
        payway: e.detail.value
      });
    },
    subMitButn: function () {
      var that = this;
      let money = this.money;
      let payWay = this.payway;
      let wxChatPayParam = {
        rechargeAmount: '',
        payType: 3
      };
      wxChatPayParam.rechargeAmount = money;
      wxChatPayParam.payType = payWay;
      console.log(wxChatPayParam);
      this.setData({
        butn_show_loading: true
      });
      let customIndex = getApp().globalData.AddClientUrl("/create_recharge_order.html", wxChatPayParam, 'post');
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log(res.data); //这里拿到订单数据
          //下面应该吊起支付

          console.log(res.data);
          let orderNo = res.data.orderNo;

          if (!res.data || !res.data.payType) {
            console.log('--------失败-------');
          }

          if (res.data.payType == 3) {
            that.payByWechat(orderNo);
          }
        },
        fail: function () {},
        complete: function () {
          that.setData({
            butn_show_loading: true
          });
        }
      });
    },
    payByWechat: function (orderNo) {
      var that = this;
      let loginUser = getApp().globalData.loginUser;
      console.log(loginUser);
      if(!loginUser){
          getApp().globalData.addLoginListener(that);
          let url='login.html?url=';
          let backUrl='user_recharge.html';
          url+= encodeURIComponent(backUrl)
          getApp().globalData.linkEvent(url)
          return
      }
      let wxChatPayParam = {
        openid: '',
        orderNo: '',
        app: 3
      };
      wxChatPayParam.openid = loginUser.platformUser.miniOpenId;
      wxChatPayParam.orderNo = orderNo;
      console.log(wxChatPayParam);
      let customIndex = getApp().globalData.AddClientUrl("/unifined_order.html", wxChatPayParam, 'post');
      wx.request({
        url: customIndex.url,
        data: customIndex.params,
        header: getApp().globalData.headerPost,
        method: 'POST',
        success: function (res) {
          console.log(res.data);
          let PayStr = res.data;
          PayStr = '{' + PayStr + '}';
          let wechatPayStr = JSON.parse(PayStr);
          console.log(wechatPayStr);
          wx.requestPayment({
            'timeStamp': wechatPayStr.timeStamp,
            'nonceStr': wechatPayStr.nonceStr,
            'package': wechatPayStr.package,
            'signType': wechatPayStr.signType,
            'paySign': wechatPayStr.paySign,
            'success': function (res) {
              console.log('------成功--------');
              console.log(res);
              wx.showToast({
                title: '支付成功',
                icon: 'success',
                duration: 2000
              });
            },
            'fail': function (res) {
              console.log('------fail--------');
              console.log(res);
              wx.showToast({
                title: '支付失败',
                image: '/wxcomponents/images/icons/tip.png',
                duration: 2000
              });
            },
            'complete': function () {
              console.log('------complete--------');
              console.log(res);
              that.setData({
                butn_show_loading: false
              });
              getApp().globalData.navigateBack(2000);
            }
          });
        }
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
@import "./index.css";
</style>